/**
 * JAVACC DEMO 1.0
 */
package com.apache.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.info.entity.PubMetadata;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;

/**
 * description:  动态信息客户调用中心
 *
 * @author Hou Dayu 创建时间：2016-8-29
 */
public class InfoReleaseCoreCleint {

	private static InfoReleaseCoreCleint instance;

	private static final String serviceName = "infoService";

	private InfoReleaseCoreCleint() {
	}

	public static InfoReleaseCoreCleint getInstance() {
		if (null == instance) {
			instance = new InfoReleaseCoreCleint();
		}
		return (instance);
	}

	/**
	 * description:  保存信息
	 */
	public ResultMsg saveMasterSlaveInfo(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "subInsert", map);
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "保存成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  修改信息
	 */
	public ResultMsg updateMasterSlaveInfo(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "subUpdate", map);
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "操作成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  删除信息
	 */
	public ResultMsg deleteMasterSlaveInfo(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "subDel", map);
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "操作成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  查询信息列表
	 */
	public Object selectMasterSlaveInfo(Map map) {
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "subSelect", map);
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			return entity.getEntity();
		}
		return null;
	}

	/**
	 * description:  查询单个信息
	 */
	public Object selectMasterSlaveSingle(Map map) {
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "subSingle", map);
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			return entity.getEntity();
		}
		return null;
	}

	/**
	 * description:  查询单个信息
	 */
	public long countMasterSlaveInfo(Map map) {
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "subCount", map);
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			return Long.valueOf(String.valueOf(entity.getEntity()));
		}
		return 0;
	}

	/**
	 * description:  查询单个信息
	 */
	public long countStatistics(Map map) {
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "infoStatistics", map);
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			return Long.valueOf(String.valueOf(entity.getEntity()));
		}
		return 0;
	}

	/**
	 * description:  保存信息
	 */
	public ResultMsg saveInfo(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "insertInfo", map);
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "保存成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  修改信息
	 */
	public ResultMsg updateInfo(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "updateInfo", map);
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "操作成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  删除信息
	 */
	public ResultMsg deleteInfo(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "deleteInfo", map);
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "操作成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  查询信息列表
	 */
	public Object selectInfo(Map map) {
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "selectInfo", map);
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			return entity.getEntity();
		}
		return null;
	}

	/**
	 * description:  查询单个信息
	 */
	public Object selectSingle(Map map) {
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "selectSingle", map);
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			return entity.getEntity();
		}
		return null;
	}

	/**
	 * description:  查询单个信息
	 */
	public long countInfo(Map map) {
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "countInfo", map);
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			return Long.valueOf(String.valueOf(entity.getEntity()));
		}
		return 0;
	}

	/**
	 * description:  获取数据元素列表
	 */
	public List<PubMetadata> getMetadatas(String sortCode, String sysName) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sortCode", sortCode);
		map.put("sysName", sysName);
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "findMetadatas", map);
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			if (entity.getEntity() instanceof ArrayList) {
				return (List) entity.getEntity();
			}
		}
		return null;
	}

	/**
	 * description:  获取数据主键元素信息
	 */
	public PubMetadata getMetadataPrimarykey(String sortCode, String sysName) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sortCode", sortCode);
		map.put("sysName", sysName);
		String sysPass = ConfigUtil.getInstance().interfacePass();
		map.put("sysPass", sysPass);
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "findMetadataPrimarykey", map);
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			if (entity.getEntity() instanceof PubMetadata) {
				return (PubMetadata) entity.getEntity();
			}
		}
		return null;
	}

	/**
	 * description:  获取指定代号的子表代号列表
	 */
	@SuppressWarnings("unchecked")
	public List<String> getMetadataForeignKeys(String sortCode, String sysName) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("foreignKeyCode", sortCode);
		map.put("sysName", sysName);
		String sysPass = ConfigUtil.getInstance().interfacePass();
		map.put("sysPass", sysPass);
		ResultEntity entity = UctProtoclClient.getInstance().doInfoService(serviceName, "findMetadataForeignkey", map);
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			if (entity.getEntity() instanceof ArrayList) {
				return (List<String>) entity.getEntity();
			}
		}
		return null;
	}

}
